﻿using System;
using System.IO;
using Moonlite.ExtensionApi.XimlInterfaces;

namespace Moonlite.Platform.Ximl
{
    public class IncludeNodeDataProvider : INodeDataProvider
    {
        bool INodeDataProvider.ParseXimlNode(ref IXimlNode node, out DescriptorBase descriptor)
        {
            descriptor = null;
            if (node.Name == "Include")
            {
                if (XimlTree.Instance.CurrentAssembly == null)
                {
                    throw new NoAssemblyContextException("No valid assembly context found.");
                }
                Stream stream = ResourceManager.GetStream(node["path"], XimlTree.Instance.CurrentAssembly);
                using (StreamReader reader = new StreamReader(stream))
                {
                    XimlTree.Instance.BuildItem(reader.ReadToEnd(), true);
                }
            }
            return false;
        }
    }
}
